package org.glut.service;

import org.glut.bean.GZ;
import org.glut.serialport.manager.SerialPortManager;
import org.glut.serialport.ui.MainFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Scope("singleton")
public class GZService {
    @Autowired
    MessageService messageService;
    //    @Autowired
//    private  static MainFrame mainFrame;
    @Autowired
    public static GZ gz1;
    public static GZ gz2;
    public List<String> gztimeList1 = new ArrayList<>();
    public List<String> gznumList1 = new ArrayList<>();
    public List<String> gzpower1 = new ArrayList<>();
    public List<String> gztimeList2 = new ArrayList<>();
    public List<String> gznumList2 = new ArrayList<>();
    public List<String> gzpower2 = new ArrayList<>();
    private SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置时间

    public GZService() {
        gz1 = new GZ();
        gz2 = new GZ();
//        (GZ)GetApplicationContext.getBean("GZ")
    }


    public void getmsgs(String[] message) {
//        for (String s : message) {
//            System.out.println(s);
//        }
//        gz = (GZ) GetApplicationContext.getBean("GZ");
        char id = message[0].charAt(2);
        if (id == '1') {
            initCF(gz1,message,gztimeList1,gznumList1,gzpower1);
        } else if (id == '2') {
            initCF(gz2,message,gztimeList2,gznumList2,gzpower2);
        }


//            System.out.println(timeList);
//            System.out.println(gzList);
    }

    private void initCF(GZ gz, String[] message, List<String> gztimeList, List<String> gznumList, List<String> gzpower) {
//        GZ1 B2333 L203000 f013 S1E
        gz.setId(message[0].charAt(2)+"");
        gz.setPower(message[1].substring(1));
        String substring = message[2].substring(1,6);
        gz.setCurNum(substring+"."+message[2].charAt(6));
        gz.setMax(message[3].substring(1));
        gz.setStatus(message[4].substring(1));
        String time = df.format(new Date());
        gztimeList.add(time);
        gznumList.add(gz.getCurNum());
        gzpower.add(gz.getPower());
    }

    /**
     * 发送命令控制光照度
     * @param num
     * @param auto
     * @param status
     */
    public void setCmd(String num, String auto, String status,String id) {
//        GZ2 M0 F013 S1E
        String cmd = "GZ"+ id+ " M" +auto+" F"+ num + " S" + status+"E";
        System.out.println(cmd);
        if (messageService.outputStream != null) {
            try {
                messageService.outputStream.write(cmd.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println(cmd);
//            System.out.println("------socket未连接，outputStream为空------");
        }
//        mainFrame = (MainFrame) GetApplicationContext.getBean("MainFrame");

        try {
            if (MainFrame.mSerialport != null) {
                SerialPortManager.sendToPort(MainFrame.mSerialport, cmd.getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
